import 'dart:convert';

import 'package:crypto/crypto.dart';
import 'package:flutter/material.dart';

///author: create by SunJH
///时间: 2022/1/24 0024
///邮箱: 965989158@qq.com
///描述: 参数签名测试页面

class SignArgsPage extends StatelessWidget {
  const SignArgsPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var demo = "uOVRnA4qG/MNnYzdQxJanN+zU+lTgIcnU9BxGw5dKjK+VdEUz2FeIoC+D5sB/LN+nGzX3hfZg6r5wT1pl2ZobmIc6p0ldN7J6yDgUzbX8Uk3sD4a4eZVPTBvqNDoUqcYMlZ9uuDdCvNv4TM3c1WzsXUrExwVkI1XO5jCNbgDJ25nkT/c1gIFvqoogl7MdSFGc4W4xZsqCItnqbypR3RuGIlR9h9vlRsy7zJR9PBI83X8alLDIfR1ukt1P7tMnmogZ0cuDY8cZsd8ZlCgLadmvej58SLsIkVxFJ8XyUgx9FmutKSYTmYtWBZ0+tNvfGmbXU7cob8H/4nLBiCwIUFluw==";
    return Scaffold(
      body: Center(
        child: TextButton(
          child: Text("进行签名"),
          onPressed: (){
            signParams();
          },
        ),
      ),
    );
  }

  void signParams() {
    var appid = "wx6299240699200b38";
    var timeStamp = "1642988094";
    var nonceStr = "MU9ihTsYLGL8Kmp2a2jJopaxGNZuU7UE";
    var prepayId = "wx24093454953077565f76f34a68054e0000";

    var stringBuffer = StringBuffer();
    stringBuffer.write(appid);
    stringBuffer.write("\n");
    stringBuffer.write(timeStamp);
    stringBuffer.write("\n");
    stringBuffer.write(nonceStr);
    stringBuffer.write("\n");
    stringBuffer.write(prepayId);
    stringBuffer.write("\n");

    var key = utf8.encode('1613423097');
    var bytes = utf8.encode(stringBuffer.toString());

    var hmacSha256 = Hmac(sha256, key); // HMAC-SHA256
    var digest = hmacSha256.convert(bytes);

    print("HMAC digest as hex string: $digest");
    var bytes1 = utf8.encode(digest.toString());

    var encoded1 = base64Encode(bytes1);
    print(encoded1);
  }
}
